package com.example.demo.error;

import com.example.demo.code.CustomStatusCode;
import lombok.Getter;

@Getter
public class ApiResponse {
    private final long timestamp;
    private int code;
    private String msg;
    private Object data;
    private int dataSize;

    public ApiResponse(CustomStatusCode status, Object data, int dataSize) {
        this.code = status.getCode();
        this.msg = status.getDescription();
        this.data = data;
        this.dataSize = dataSize;
        this.timestamp = System.currentTimeMillis();
    }

    public void setCode(int code) {
        this.code = code;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public void setDataSize(int dataSize) {
        this.dataSize = dataSize;
    }

    @Override
    public String toString() {
        return "ApiResponse{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                ", dataSize=" + dataSize +
                '}';
    }

    public static Builder builder() {
        return new Builder();
    }

    public static class Builder {
        private int code;
        private String msg;
        private Object data;
        private int dataSize;

        public Builder code(int code) {
            this.code = code;
            return this;
        }

        public Builder msg(String msg) {
            this.msg = msg;
            return this;
        }

        public Builder data(Object data) {
            this.data = data;
            return this;
        }

        public Builder dataSize(int dataSize) {
            this.dataSize = dataSize;
            return this;
        }

        public ApiResponse build() {
            return new ApiResponse(code, msg, data, dataSize);
        }
    }

    public ApiResponse(int code, String msg, Object data, int dataSize) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.dataSize = dataSize;
        this.timestamp = System.currentTimeMillis();
    }
}